
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

import javax.xml.XMLConstants;

/**
 * 使用serviceClient进行xml定制发送
 * User: Francis.Hu
 * Date: 13-12-4
 * Time: 下午1:41
 */
public class BaseUtil {
    public static void main(String[] args) {
        String url = "http://222.222.250.242:8081/cmcswebservice/services/emsSource";

        String Username = "hbsdqhj";
        String Password = "5LCHep=&CH@S4o6o";


        String in0 = "46407";
        Long in1 = 1567482055L;
        Long in2 = 1567482475L;


        try {
            Options options = new Options();

            // 指定调用WebService的URL
            EndpointReference targetEPR = new EndpointReference(url);
            options.setTo(targetEPR);

            //client
            ServiceClient sender = new ServiceClient();
            sender.setOptions(options);

            OMFactory factory = OMAbstractFactory.getOMFactory();
            OMNamespace omNs =  factory.createOMNamespace("http://www.labwayit.com/cmcswebservice","");

            //头消息指定
            OMElement header = factory.createOMElement("Authentication",omNs);

            OMElement sn = factory.createOMElement("Username", omNs);
            sn.setText(Username);
            header.addChild(sn);

            OMElement sv = factory.createOMElement("Password", omNs);
            sv.setText(Password);
            header.addChild(sv);


            sender.addHeader(header);

            //方法的定义，namespace和后面m0的效果，参考下面报文示例
            OMNamespace bodyNM =  factory.createOMNamespace("http://DefaultNamespace","def");
            OMElement method = factory.createOMElement("fetchRealTimeData", bodyNM);

            //方
            OMElement destTermXML = factory.createOMElement("in0", bodyNM);
            destTermXML.setText(in0);
            method.addChild(destTermXML);
            //方法里的参数2，下面依次类推
            OMElement srcTermIDXML = factory.createOMElement("in1", bodyNM);
            srcTermIDXML.setText(in1+"");
            method.addChild(srcTermIDXML);

            OMElement msgContentXML = factory.createOMElement("in2", bodyNM);
            msgContentXML.setText(in2+"");
            method.addChild(msgContentXML);

            //构建request报文
            method.build();

            //发送报文请求，返回OMElement
            System.out.println(method);
            OMElement result = sender.sendReceive(method);

            System.out.println(result);
        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

}